home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 September / Australian PC User - September 2003 (CD1).iso / magstuff / web / files / dwmx61.exe / Disk1 / data1.cab / Configuration_En / Commands / TemplateParams.js < prev    next >
Encoding:
JavaScript  |  2002-11-25  |  14.3 KB  |  537 lines

  1. //  Copyright 2001 Macromedia, Inc. All rights reserved.
  2.  
  3. //form fields:
  4. //Background - multiple list listing background colors
  5. //Text - multiple list listing text colors. Contents change when new Background item picked.
  6.  
  7. // ******************* GLOBALS **********************
  8.  
  9. var helpDoc = MM.HELP_templateProperties;
  10.  
  11. //This is the data passed into the dialog, and this is used to return data as well. If declared in C, this 
  12. //object would look something like this: 
  13. //struct { 
  14. //        values;                         //Set of all the template parameters that can be changed 
  15. //                                        //in this dialog. This is an object where the attributes
  16. //                                        //are the names of the params, and the values 
  17. //                                        //are the param values.
  18. //        types;                            //The types for all the params in the dialog. This is an
  19. //                                        //object, the attributes are the template attributes, and 
  20. //                                        //the values are either "boolean", "number", "color" , "text", "URL"
  21. //                                        // NOTE: 'CHOICES' IS NOT CURRENTLY SUPPORTED
  22. //        passthrough;                    //object containing a boolean for each attribute, whether it is passthrough or not.
  23. //        returnValue;                    //passed in as -1, set to 1 for success, 0 for cancel. 
  24. //        };
  25.  
  26. var dialogData = null;
  27. var isFake = false; //Used for debugging 
  28.  
  29. var PLATFORM = navigator.platform;
  30.  
  31. //I cache the DOM objects for the layers, so I don't have to search for them all the time. 
  32. var boolControlLayer = null; 
  33. var numberControlLayer = null; 
  34. var colorControlLayer = null; 
  35. var textControlLayer = null; 
  36. var linkControlLayer = null; 
  37. var noControlLayer = null; 
  38. var passthroughSpan = null; 
  39. var passthroughSpanHTML = null; 
  40.  
  41. var targetLayer = null; 
  42.  
  43. //This is the currently visible control layer. 
  44. var curVisibleLayer = null;
  45.  
  46. var errorPending = false; 
  47.  
  48. // ******************* API **********************
  49.  
  50. //--------------------------------------------------------------------
  51. // FUNCTION:
  52. //   commandButtons
  53. //
  54. // DESCRIPTION:
  55. //   The list of buttons to display on the right of the dialog,
  56. //   along with the functions to call when they are pressed.
  57. //
  58. // ARGUMENTS:
  59. //   none
  60. //
  61. // RETURNS:
  62. //   javascript array
  63. //--------------------------------------------------------------------
  64.  
  65. function commandButtons()
  66. {
  67.     return new Array(MM.BTN_OK,     "cmdOK();",
  68.                    MM.BTN_Cancel, "cmdCancel()",
  69.                    MM.BTN_Help,   "displayHelp();");
  70. }
  71.  
  72.  
  73. function isDomRequired() {return true;} 
  74.  
  75. //Just grab the first argument and stuff it into a global. 
  76. function receiveArguments()
  77.     {        
  78.     dialogData = dw.getDocumentDOM().getTemplateParameters();
  79.     }
  80.  
  81. function canAcceptCommand()
  82.     {    
  83.     var curDOM = dw.getDocumentDOM(); 
  84.     
  85.     if (curDOM == null)
  86.         return false; 
  87.         
  88.     return (curDOM.getAttachedTemplate().length > 0);
  89.     } //canAcceptCommand
  90.     
  91. function cmdOK()
  92.     {
  93.     if (!isFake)
  94.         {
  95.         var curDOM = dw.getDocumentDOM();
  96.         curDOM.disableLocking();
  97.         if (!curDOM.setTemplateParameters(dialogData))
  98.             return; 
  99.         }
  100.         
  101.     if (errorPending)
  102.         return; 
  103.         
  104.     dialogData.returnValue = 1; 
  105.     window.close();
  106.     } //cmdOK
  107.  
  108. function cmdCancel()
  109.     {
  110.       dialogData.returnValue = 0; 
  111.       window.close();
  112.     } //cmdCancel
  113.  
  114.     
  115.  
  116. //***************** LOCAL FUNCTIONS  ******************
  117.     
  118. function initializeUI()
  119.     {
  120.     errorPending = false; 
  121.     if (dialogData == null)
  122.         {
  123.         window.close(); 
  124.         //alert(MSG_CantRun); 
  125.         return; 
  126.         }
  127.         
  128.     //Find the layer objects. 
  129.     boolControlLayer = findObject("booleanControls", null);     
  130.     numberControlLayer = findObject("numberControls", null); 
  131.     colorControlLayer = findObject("colorControls", null); 
  132.     textControlLayer = findObject("textControls", null); 
  133.     linkControlLayer = findObject("linkControls", null); 
  134.     noControlLayer = findObject("noControls", null); 
  135.  
  136.     passthroughSpan = findObject("passthroughSpan", null); 
  137.   
  138.   // find the table
  139.   var tableObj = findObject("mainTable");
  140.   if (dw.isOSX()){
  141.     tableObj.setAttribute("height","300");
  142.   }
  143.     
  144.     if (passthroughSpanHTML == null) 
  145.         passthroughSpanHTML = passthroughSpan.innerHTML; 
  146.     
  147.     targetLayer = findObject("visibleSpan", null);
  148.     curVisibleLayer = noControlLayer; 
  149.     
  150.       SetupTree();
  151.       SetupControls();
  152.     window.resizeToContents(); 
  153.     
  154.       //FixControlLayers(null);
  155.     } //initializeUI
  156.  
  157. function adjustButtons()
  158. {
  159.   var theStyle;
  160.   if (PLATFORM=="Win32")
  161.   {
  162.     theStyle=document.okLayer.getAttribute("STYLE");
  163.     theStyle=theStyle.replace(/left:\w*;/,LEFT_LAYER).replace(/top:\w*;/,TOP_LAYER);
  164.     document.okLayer.setAttribute("STYLE", theStyle);
  165.     theStyle=document.cancelLayer.getAttribute("STYLE");
  166.     theStyle=theStyle.replace(/left:\w*;/,RIGHT_LAYER).replace(/top:\w*;/,TOP_LAYER);
  167.     document.cancelLayer.setAttribute("STYLE", theStyle);
  168.   }
  169. }
  170.  
  171. //Add the 'categories' tag to the string stream
  172. function AddCategoriesToTreeStream(theStream)
  173.     {
  174.     theStream.push("<mm:treecolumn name='", LABEL_NameCol, "' value='", LABEL_NameCol, "'  width='200'/>"); 
  175.     theStream.push("<mm:treecolumn name='", LABEL_ValueCol, "' value='", LABEL_ValueCol, "' width='130'/>"); 
  176.     } //AddCategoriesToTreeString
  177.     
  178.     
  179. //Add the treenode for this category to the innerHTML string for the tree node
  180. function OpenCatNode(labelString, theStream)
  181.     {
  182.     theStream.push("<mm:treenode selected value = '",labelString,"' id=-1 state='expanded'>");
  183.     } //AddCatNodeToString
  184.     
  185. function CloseCatNode(theStream)
  186.     {
  187.     theStream.push("</mm:treenode>");
  188.     }
  189.  
  190. function GetVisibleValueString(paramName)
  191.     {
  192.     if (dialogData == null)
  193.         return "";
  194.         
  195.     if (dialogData.passthrough[paramName])
  196.         return LABEL_Passthrough; 
  197.         
  198.     if (dialogData.types[paramName] == "boolean")
  199.         {
  200.         var paramValue = dialogData.values[paramName]; 
  201.         return (paramValue == "true") ? LABEL_TRUE : LABEL_FALSE; 
  202.         }
  203.         
  204.     return dialogData.values[paramName];
  205.     } //GetVisibleValueString
  206.     
  207.     
  208. //Add the string for a single node in the tree. Should look something like: 
  209. //<mm:treenode  value = "Conditional 1|<Not Resolved>" paramName="some name" valueType="number" ></mm:treenode> 
  210. function AddParamNode(paramName, theStream, selected)
  211.     {    
  212.     //var selectedString = selected ? "selected" : ""; 
  213.     var selectedString = ""; 
  214.     
  215.     theStream.push("<mm:treenode  value = '", paramName,  "|", GetVisibleValueString(paramName),  
  216.                       "' paramName='", paramName, "' valueType='", dialogData.types[paramName], "' ", selectedString , " ></mm:treenode>"); 
  217.     } //AddParamNode
  218.     
  219.     
  220. //Build and insert the inner HTML for the tree control
  221. function SetupTree()
  222.     {    
  223.     var theStream = new Array();
  224.     theStream.push("");
  225.      
  226.     AddCategoriesToTreeStream(theStream);
  227.     
  228.     var i; 
  229.     var count = 0; 
  230.     
  231.     //Sort the list of params alphabetically. 
  232.     var paramNames = new Array(); 
  233.     for (i in dialogData.values)
  234.         paramNames.push(i); 
  235.     paramNames.sort();
  236.     
  237.     //Add them to the tree
  238.     for (i=0; i<paramNames.length; i++)
  239.         {
  240.         AddParamNode(paramNames[i], theStream, count==0);    
  241.         count++;
  242.         }
  243.     
  244.     if (count == 0)
  245.         theStream.push("<mm:treenode  value = '" + LABEL_NoParams + " | '     paramName='bar' valueType='none' ></mm:treenode>");
  246.             
  247.     //var theTree = findObject("theTreeControl", null); 
  248.     var theTree = dwscripts.findDOMObject("theTreeControl", null); 
  249.     theTree.innerHTML = theStream.join(""); 
  250.       if (theTree.treeNodes.length > 0){
  251.         theTree.treeNodes[0].selected = true;
  252.       }
  253. } //SetupTree
  254.     
  255.  
  256.     
  257. //Everything is off unless on
  258. function FixControlLayers(selectedTreeNode)
  259.     {        
  260.     if (selectedTreeNode == null || selectedTreeNode.valueType == 'none')
  261.         {                    
  262.         targetLayer.innerHTML = noControlLayer.innerHTML; 
  263.         curVisibleLayer = noControlLayer;     
  264.         passthroughSpan.innerHTML = ""; 
  265.         return;     
  266.         }
  267.     
  268.     passthroughSpan.innerHTML = passthroughSpanHTML;
  269.     if (dialogData.passthrough[selectedTreeNode.paramName])
  270.         {
  271.         targetLayer.innerHTML = LABEL_Passthrough; 
  272.         document.theForm.passthroughCheck.checked = true; 
  273.         curVisibleLayer = noControlLayer;
  274.         return;      
  275.         }
  276.         
  277.     var newActiveLayer = noControlLayer; 
  278.     document.theForm.passthroughCheck.checked = false; 
  279.  
  280.     switch (selectedTreeNode.valueType)
  281.         {
  282.         case "boolean":
  283.             newActiveLayer = boolControlLayer;  break;
  284.         case "number":
  285.             newActiveLayer = numberControlLayer;  break;
  286.         case "color":
  287.             newActiveLayer = colorControlLayer; break;
  288.         case "text":
  289.             newActiveLayer = textControlLayer;  break;
  290.         case "link":
  291.         case "URL":
  292.             newActiveLayer = linkControlLayer; break;
  293.         }
  294.         
  295.     if (curVisibleLayer != newActiveLayer)
  296.         {
  297.         targetLayer.innerHTML = newActiveLayer.innerHTML; 
  298.         curVisibleLayer = newActiveLayer; 
  299.         }        
  300.     
  301.     //Customize the string for the selected value
  302.     var showString = selectedTreeNode.paramName; 
  303.     if (selectedTreeNode.valueType == "boolean")
  304.         showString = errMsg(LABEL_Show, selectedTreeNode.paramName);
  305.         
  306.     var labelSpan = findObject("propLabel", null); 
  307.     if (labelSpan != null)
  308.         labelSpan.innerHTML = showString; 
  309. } //FixControlLayers
  310.         
  311.  
  312. //Move the values currently stored in this parameter into the controls for it's type. 
  313. function MoveValuesToControl(selectedNode)
  314.     {
  315.     if (selectedNode == null || selectedNode.valueType == 'none')
  316.         return; 
  317.     
  318.     var attrName = selectedNode.paramName; 
  319.     if (dialogData.passthrough[attrName])
  320.         return; //No controls in this case 
  321.         
  322.     var controlName = ""; 
  323.     var controlObj = null;
  324.     switch (selectedNode.valueType)
  325.         {
  326.         case "boolean": controlName = "booleanCheck";     break;
  327.         case "number":     controlName = "numberField";     break; 
  328.         case "color":     controlName = "colorField";      break;
  329.         case "text":     controlName = "textField";          break;    
  330.         
  331.         case "link":     
  332.         case "URL":     
  333.             controlName = "linkField";      break;
  334.         }
  335.     
  336.     if (controlName == "")
  337.         return; 
  338.         
  339.     //Select the control...
  340.     var controlObj = findObject(controlName, null); 
  341.     
  342.     if (controlObj != null)
  343.         {
  344.         
  345.         //If this is a text control, move the focus into it. 
  346.         if (controlName == "booleanCheck")    
  347.             controlObj.checked = (dialogData.values[attrName] == "true");
  348.         else
  349.             {
  350.             controlObj.value = dialogData.values[attrName];
  351.             if (typeof controlObj["focus"] != "undefined")
  352.                   controlObj.focus(); //set focus on textbox
  353.                   
  354.             if (typeof controlObj["select"] != "undefined")
  355.                   controlObj.select(); //set insertion point into textbox
  356.             }
  357.             
  358.         if (selectedNode.valueType == "color")
  359.             {
  360.             controlObj = findObject("colorPicker", null); 
  361.             if (controlObj != null)
  362.                 controlObj.value = dialogData.values[attrName];
  363.             }
  364.         }
  365.  
  366.     } //MoveValuesToControl
  367.     
  368.         
  369.     
  370. // changes the 'resolve' popup to show the possibilities for the current node
  371. function SetupControls()
  372.     {
  373.     var theTree = findObject("theTreeControl", null); 
  374.     
  375.     var selectedTreeNode = theTree.selectedNodes[0];    
  376.     FixControlLayers(selectedTreeNode);
  377.     MoveValuesToControl(selectedTreeNode);
  378.     
  379.     if (selectedTreeNode)
  380.         selectedTreeNode.selected = true; //some bug in the tree control...
  381.     } //SetupControls
  382.  
  383. //Once the user picks an entry from the list, store it back in the dialogData object
  384. function StoreNodeValue()
  385.     {
  386.     var selectedNode = findObject("theTreeControl", null).selectedNodes[0];
  387.     if (selectedNode == null)
  388.         return false; 
  389.         
  390.     var controlObj = null; 
  391.     var attrName = selectedNode.paramName; 
  392.     
  393.     switch (selectedNode.valueType)
  394.         {
  395.         case "boolean":
  396.             {
  397.             controlObj = findObject("booleanCheck", null);         
  398.             var isChecked = false; 
  399.             if (typeof    controlObj.checked == "string")
  400.                 isChecked = (controlObj.checked == "true"); 
  401.             else
  402.                 isChecked = controlObj.checked; 
  403.                 
  404.             if (controlObj != null)
  405.                 dialogData.values[attrName] = isChecked ? "true" :  "false";
  406.             break;
  407.             }
  408.             
  409.         case "number":
  410.             {
  411.             controlObj = findObject("numberField", null); 
  412.             if (controlObj != null)
  413.                 {
  414.                 var controlValue = controlObj.value; 
  415.                 if (isNaN(controlValue))
  416.                     {
  417.                     if (errorPending)
  418.                         return false;  
  419.  
  420.                     alert(MSG_isNAN);
  421.                     errorPending = true; 
  422.  
  423.                     return false; 
  424.                     }
  425.                 dialogData.values[attrName] = controlObj.value;
  426.                 }
  427.                 
  428.             break;
  429.             }
  430.  
  431.         case "color":
  432.             {
  433.             controlObj = findObject("colorField", null); 
  434.             if (controlObj != null)
  435.                 dialogData.values[attrName] = controlObj.value;
  436.             break;
  437.             }
  438.             
  439.         case "text":
  440.             {
  441.             controlObj = findObject("textField", null); 
  442.             if (controlObj != null)
  443.                 dialogData.values[attrName] = controlObj.value;
  444.             break;
  445.             }
  446.             
  447.         case "URL":
  448.         case "link":
  449.             {
  450.             controlObj = findObject("linkField", null); 
  451.             if (controlObj != null)
  452.                 dialogData.values[attrName] = controlObj.value;
  453.             break;
  454.             }
  455.         }
  456.         
  457.     errorPending = false;     
  458.     dialogData.passthrough[attrName] = document.theForm.passthroughCheck.checked;
  459.     
  460.     var newNodeString = attrName + "|" + GetVisibleValueString(attrName); 
  461.     selectedNode.value = newNodeString; 
  462.     return true; 
  463.     } //StoreNodeValue
  464.     
  465.     
  466.     
  467. //Called when one of the controls changes, this either resets the popup menu to reflect the current 
  468. //selected node, or stores the resolution in the tree and data store
  469. function updateUI(itemName)
  470.     {
  471.       switch(itemName)
  472.           {
  473.           case "theTreeControl":
  474.               {
  475.             SetupControls();
  476.  
  477.             break;
  478.               }
  479.               
  480.         case "colorField": 
  481.           case "colorPicker":
  482.               {              
  483.               //If the user types into the color field, move the data into the colorPicker field, 
  484.               //and then store the value. 
  485.             var fieldObj = findObject("colorField", null); 
  486.             var pickerObj = findObject("colorPicker", null); 
  487.             
  488.             if (fieldObj != null && pickerObj != null)
  489.                 {
  490.                 if (itemName == "colorField")
  491.                     pickerObj.value = fieldObj.value; 
  492.                 else
  493.                     {
  494.                     //alert("setting field to " + pickerObj.value);
  495.                     fieldObj.value = pickerObj.value; 
  496.                     }
  497.                 }
  498.                 
  499.             StoreNodeValue();
  500.             break; 
  501.               }
  502.           
  503.           case "boolCheck":
  504.           case "textField":
  505.           case "linkField": 
  506.           case "numberField": 
  507.               {
  508.               StoreNodeValue();
  509.             break;
  510.               }
  511.         
  512.         case "passthroughCheck": 
  513.             {
  514.             var selectedNode = findObject("theTreeControl", null).selectedNodes[0];
  515.             if (selectedNode == null)
  516.                 return; 
  517.                                 
  518.             var attrName = selectedNode.paramName; 
  519.             var isPassthrough = document.theForm.passthroughCheck.checked; 
  520.             dialogData.passthrough[attrName] = isPassthrough;
  521.             
  522.             var newNodeString = attrName + "|" + GetVisibleValueString(attrName); 
  523.             selectedNode.value = newNodeString; 
  524.  
  525.             FixControlLayers(selectedNode);
  526.                         
  527.             //Pull the old values out.
  528.             if (!isPassthrough)
  529.                 MoveValuesToControl(selectedNode);
  530.  
  531.             break; 
  532.             }
  533.                 
  534.           } //switch
  535.     } //updateUI
  536.  
  537.